<?php
/**
 * RedCross Application
 *
 * @copyright Copyright (c) 2010, Kristian Stokke Tryggestad, Rune Vikestad, Andreas Flaten Wist, Simen Nicolai Echholt, Ole Henrik Jahren
 */

/**
 * Password Reset Service class for communication between controllers and persistent storage
 *
 * @category    RedCross
 * @package     RedCross_Service
 */
class Core_Service_PasswordReset extends RedCross_Service_ServiceAbstract
{
    private $_mailer;
    private $_userGateway;

    /**
     * Constructor
     * Assigns values to local variables
     *
     * @param Core_Gateway_PasswordReset $gateway
     * @param Core_Gateway_User $userGateway
     * @param Zend_Mail $mailer
     */
    public function __construct(Core_Gateway_PasswordReset $gateway, Core_Gateway_User $userGateway, Zend_Mail $mailer)
    {
        $this->_mailer = $mailer;
        $this->_userGateway = $userGateway;
        parent::__construct($gateway, 'Core_Model_PasswordReset');
    }

    /**
     * Method for creating a new password reset request
     * This request will be stored in the database
     *
     * @param array $values an array containing the email and username of the user
     */
    public function createRequest(array $values)
    {
        $user = $this->_userGateway->fetchByEmail($values['email']);
        if (!$user) return false;
        if (!$user->isConfirmed()) return false;

        $class = $this->getModel();
        $request = new $class();
        $request->user_id = $user->id;
        $request->expiration = $request->createExpirationTime();
        $request->reset_key = $request->createResetKey();
        $this->getGateway()->save($request);

        $this->sendLostPasswordMail($user, $request);
        return true;
    }

    /**
     * Method for sending the lost password email to a user
     *
     * @param Core_Model_User $user
     * @param Core_Model_PasswordReset $request
     */
    private function sendLostPasswordMail(Core_Model_User $user, Core_Model_PasswordReset $request)
    {
        $content = new Zend_View();
        $content->setScriptPath(APPLICATION_PATH . '/modules/core/views/emails/');

        $content->assign('username', $user->username);
        $content->assign('user_id', $user->id);
        $content->assign('reset_key', $request->reset_key);

        $bodyText = $content->render('lost-password.phtml');
        $this->_mailer->setBodyText($bodyText);
        $this->_mailer->addTo($user->getEmails(), $user->username);
        $this->_mailer->setSubject('Glemt Passord-forespørsel');
        $this->_mailer->send();
    }

    /**
     * Fetches a reset password request from persistent storage
     *
     * @param int $userId
     * @param string $resetKey
     */
    public function fetchRequest($userId, $resetKey)
    {
        return $this->getGateway()->fetchRequest($userId, $resetKey);
    }

    /**
     * Deletes all reset password request for a specific user
     *
     * @param int $userId
     */
    public function deleteRequests($userId)
    {
        return $this->getGateway()->deleteRequests($userId);
    }
}